plotly is a high-level interface to plotly.js, based on d3.js which provides an easy-to-use UI to generate slick D3 interactive graphics. These interactive graphs give the user the ability to zoom the plot in and out, hover over a point to get additional information, filter to groups of points, and much more. These interactive components contribute to an engaging user experience and allows information to be displayed in ways that are not possible with static figures.
plotly is a web application for creating and sharing data visualizations. Plotly can work with several programming languages and applications including R, Python, and Microsoft Excel. We’re going to concentrate on creating different graphs with Plotly.
Interactive Graphics!
If you want to share your visualizations on https://plot.ly/ you should make an account on their site.
There are two main approaches to initialize a plotly object:
ggplotly()plot_ly() directlyggplotly() takes existing ggplot2 objects and converts them into interactive plotly graphics. That is, ggplotly() converts your static plots to an interactive web-based version!
This makes it easy to create interactive figures because we are already familiar to the ggplot2 syntax.
library(plotly)
g <- ggplot(data=incex) + geom_bar(mapping = aes(x = occ))
ggplotly(g)
g2 <- ggplot(incex) + geom_histogram(aes(x = income), bins = 20) + theme_minimal()
ggplotly(g2)
g3 <- ggplot(incex) + geom_density(aes(x = income)) + theme_minimal()
ggplotly(g3)
g4 <- ggplot(incex, aes(x = age, y = income)) + geom_point() + geom_smooth(method="loess", formula=y~x, se=F) + theme_bw()
ggplotly(g4)
plot_ly() is the base plotly command to initialize a plot from a dataframe, similar to ggplot() from ggplot2.Specify type = "bar" to create a bar plot.
More info from https://plotly.com/r/bar-charts/; https://plotly.com/r/reference/#bar
occ_counted <- incex %>% count(occ, name = 'count')
occ_counted
occ count
1 low 595
2 med. 700
3 high 627
plot_ly(occ_counted, x= ~occ, y=~count, type = "bar")
occ_counted$text <- c("low level", "medium level", "high level")
p<- plot_ly(occ_counted, x= ~occ, y=~count, type = "bar",
text = ~text,
color = I("skyblue"),
stroke = I("red"),
span = I(10))
p
# change title, xlabel, and ylabel
p %>% layout(title = "Freq. of Occupation Status",
xaxis = list(title = "Occupation Status"),
yaxis = list(title = "Frequency"))
Specify type = "box" to create a boxplot.
More info from https://plotly.com/r/box-plots/
plot_ly(incex, y = ~income, type = "box", name = '')
?airmiles
plot_ly(incex, y = ~income, color = ~occ, type = "box")
Use the type = "histogram" argument.
More info from https://plotly.com/r/histograms/
plot_ly(incex, x = ~income, type = "histogram")
Specify a scatterplot by indicating type = "scatter" and mode = "markers".
More info from https://plotly.com/r/line-and-scatter/; https://plotly.com/r/reference/#scatter; https://plotly.com/r/reference/#scatter-mode
plot_ly(incex, x = ~income, y = ~age, type = "scatter", mode = "markers")
plot_ly().plot_ly(incex, x = ~income, y = ~age, type = "scatter", mode = "markers",
color = ~edu)
plot_ly(incex, x = ~income, y = ~age, type = "scatter", mode = "markers",
color = ~edu, colors = "Set1")
set.seed(1)
index <- sample(1:nrow(incex), 100, replace=FALSE)
subincex <- incex[index, ]
plot_ly(subincex, x = ~income, y = ~age, type = "scatter", mode = "markers",
symbol = ~edu,
symbols = c('circle','x','o'),
color = I('black'),
marker = list(size = 10))
type = "scatter3d" argument. If you click and drag these scatterplots, you can view them from different angles.plot_ly(subincex, x = ~income, y = ~age, z= ~oexp, type = "scatter3d", mode = "markers", color = ~oexp, name="")
Specify a scatterplot by indicating type = "scatter" and mode = "lines".
More info from https://plotly.com/r/line-charts/; https://plotly.com/r/reference/#scatter; https://plotly.com/r/reference/#scatter-mode
Line graphs are useful for showing change over time.
For demonstration, use airmiles data that is time series data.
data("airmiles") # load airmiles data
str(airmiles) # check structure
Time-Series [1:24] from 1937 to 1960: 412 480 683 1052 1385 ...
plot_ly(x = time(airmiles), y = airmiles, type = 'scatter', mode = 'lines')